package com.light.gulimall.search.controller;

import com.light.common.enums.ExceptionEnum;
import com.light.common.pojo.ESPojo;
import com.light.common.utils.R;
import com.light.gulimall.search.service.ProductSaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/search")
public class ElasticSaveController {

    @Autowired
    private ProductSaveService productSaveService;

    /**
     * 商品上架，保存到es
     * @param esPojos
     * @return
     */
    @RequestMapping("/save")
    public R productStatusUp(@RequestBody List<ESPojo> esPojos){
        boolean result = false;
        try {
            result = productSaveService.productStatusUp(esPojos);
        } catch (IOException e) {
            return R.error(ExceptionEnum.PRODUCT_UP_EXCEPTION.getCode(),ExceptionEnum.PRODUCT_UP_EXCEPTION.getValue());
        }

        if (!result) {
            return R.ok();
        }
        return R.error(ExceptionEnum.PRODUCT_UP_EXCEPTION.getCode(),ExceptionEnum.PRODUCT_UP_EXCEPTION.getValue());
    }
}
